CREATE OR REPLACE
package body login as

PROCEDURE processLogin( 
  usernameParam IN VARCHAR2 DEFAULT NULL,
  passwordParam IN VARCHAR2 DEFAULT NULL,
  submit_button_form VARCHAR2 DEFAULT NULL
) IS
private_session VARCHAR2(512) := '';
private_status_error_message VARCHAR2(512) := '';
private_status_success_message VARCHAR2(512) := '';
BEGIN
  private_session := package_gui.processLogin( usernameParam, passwordParam );
  IF private_session IS NOT NULL THEN --errore
    private_status_success_message := 'Procedura di login andata a buon fine.';
    htp.print('<meta http-equiv="refresh" content="0;url='||ROOT_URL||'renderLogin?private_status_success_message='||private_status_success_message||'&private_session='||private_session||'">');
  ELSE
    private_status_error_message := 'Username o password invalidi. Riprova a effettuare il login.';
    private_session := '';
    htp.print('<meta http-equiv="refresh" content="0;url='||ROOT_URL||'renderLogin?private_status_error_message='||private_status_error_message||'&private_session='||private_session||'&usernameParam='||usernameParam||'&passwordParam='||passwordParam||'">');
  END IF;
END processLogin;

PROCEDURE processLogout IS
private_session VARCHAR2(512) := '';
private_status_error_message VARCHAR2(512) := '';
private_status_success_message VARCHAR2(512) := '';
BEGIN
    private_session := '';
    private_status_success_message := 'Procedura di logout andata a buon fine.';
    htp.print('<meta http-equiv="refresh" content="0;url='||ROOT_URL||'renderLogin?private_status_success_message='||private_status_success_message||'&private_session='||private_session||'">');
END processLogout;

procedure renderLogin (
  private_status_error_message VARCHAR2 DEFAULT NULL,
  private_status_success_message VARCHAR2 DEFAULT NULL,
  usernameParam VARCHAR2 DEFAULT NULL,
  passwordParam VARCHAR2 DEFAULT NULL,
  private_session VARCHAR2 DEFAULT NULL,
  submit_button_form VARCHAR2 DEFAULT NULL
) is
bc_index package_gui_statics.arrayVarcharVarchar;
sidebarLinks package_gui_statics.arrayVarcharVarchar;
validationRule package_gui_statics.ruleDefinitionType;
begin
  
  bc_index('1Home') := 'http://www.google.it';
  bc_index('2Login') := '';
  
  package_gui.newSession;
  IF private_session IS NOT NULL THEN
    package_gui.setSession(private_session);
  END IF;
  
  package_gui.renderHead ('Login');
  package_gui.openPageBody( bc_index );
  package_gui.openPageContainer;
  package_gui.openContent;
  
  /** Render CONTENT */
  package_gui.openForm( 'form-esempio', 'form-esempio', 'http://oracle3.cli.di.unipi.it/apex/RICCI.login.processLogin', 'get' );
  
  IF private_status_success_message IS NOT NULL THEN 
    package_gui.renderSuccessMessage(private_status_success_message);
  ELSE
    IF private_status_error_message IS NOT NULL THEN package_gui.renderErrorMessage(private_status_error_message); END IF;
    
    package_gui.renderTextField( TRUE, 'Username', 'usernameParam', 'usernameParam', usernameParam, 25, 255, 'Inserisci il tuo username per effettuare il login' );
    validationRule := package_gui.initRuleDefinition(TRUE);
    package_gui.addValidationRule( 'usernameParam', validationRule, NULL );
    
    package_gui.renderPasswordField( TRUE, 'Password', 'passwordParam', 'passwordParam', passwordParam, 25, 255, 'Inserisci la tua password per effettuare il login' );
    validationRule := package_gui.initRuleDefinition(TRUE);
    package_gui.addValidationRule( 'passwordParam', validationRule, NULL );
    
    
    /* Submit Button */  
    package_gui.renderSubmitButton('Login');  
  
  END IF;
  
  package_gui.closeForm;
  
  package_gui.closeContent;
  package_gui.openSidebar;
  package_gui.renderUserMenu;
  --package_gui.renderOperationsMenu( sidebarLinks );
  package_gui.closeSidebar;
  /*package_gui.closePageContainer;*/
  package_gui.closePageBody;
  
  /*Inizializzo jQuery e aggiungo tutti i controlli JS + Widgets*/
  package_gui.openjQuerySnippet;
  package_gui.closejQuerySnippet;
  
  package_gui.renderFooter;
  
end renderLogin; 



end login;

